/*****************************************************************************
 * util.c : some useful utilities
 *****************************************************************************
 * Copyright (C) 2011 PELinker
 *
 * Authors: Iceberg <iceberg58744@163.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "common.h"

int StringCompare(const String * const string1, const String * const string2)
{
	DWORD i = 0;

	if (string1->length != string2->length)
	{
		return 1;
	}

	while (i < string1->length)
	{
		if (string1->ptr[i] != string2->ptr[i])
		{
			return 1;
		}

		++i;
	}

	return 0;
}

static const BYTE print[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

void DumpDWORD(BYTE buffer[8], const DWORD arg)
{
	const BYTE *p = (const BYTE *)(&arg);

	buffer[0] = print[p[3] >> 4];
	buffer[1] = print[p[3] & 0x0f];
	buffer[2] = print[p[2] >> 4];
	buffer[3] = print[p[2] & 0x0f];
	buffer[4] = print[p[1] >> 4];
	buffer[5] = print[p[1] & 0x0f];
	buffer[6] = print[p[0] >> 4];
	buffer[7] = print[p[0] & 0x0f];
}

void PrintInfoEx(LPCTSTR Info, const DWORD len)
{
	DWORD NumberOfCharsWritten = 0;
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	WriteConsole(console, Info, len, &NumberOfCharsWritten, NULL);
}

void PrintInfo(LPCTSTR Info)
{
	DWORD len = lstrlen(Info);

	PrintInfoEx(Info, len);
}

void PrintDWORD(const DWORD arg)
{
	BYTE buffer[8];

	DumpDWORD(buffer, arg);

	PrintInfoEx(buffer, 8);
}
